import {defineStore} from "pinia";
import {ref} from 'vue';
import {
    useDeleteOneMaterialOutByIdAPI,
    useGetAllByMultifunctionNameAPI,
    useGetAllCountsByBetweenTimesAPI,
    useGetAllCountsByMultifunctionNameAPI,
    useGetAllMaterialOutAPI,
    useGetAllMaterialOutCountsAPI,
    useGetByBetweenTimesAPI,
    useGetOneByIdAPI,
    useModifyOneMaterialOutAPI,
    useSaveOneMaterialOutAPI
} from '@/apis/materialOut.ts'
import {RouteParamValue} from "vue-router";


export const useMaterialOutStore = defineStore("materialOutStore", ()=>{

//     材料出场表单数据
    const allMaterialOutList=ref([]);
    const allMaterialOutCounts=ref(0);
    const materialOutState=ref({});
    const result=ref({});


    const getAllMaterialOutStore=async (current:number,pageSize:number)=>{
        const res=await useGetAllMaterialOutAPI(current,pageSize);
        allMaterialOutList.value=res.data;
        //     下面补一个统计counts的函数

        allMaterialOutCounts.value=(await useGetAllMaterialOutCountsAPI()).data;

    }

    // 多功能名称查询接口，同时也查询所有符合名称要求的数据条数

    const getAllByMultifunctionNameStore=async (name:string,current:number,pageSize:number)=>{
        allMaterialOutList.value=(await useGetAllByMultifunctionNameAPI(name,current,pageSize)).data;
        allMaterialOutCounts.value=(await useGetAllCountsByMultifunctionNameAPI(name,current,pageSize)).data;
    }
    //  根据时间段查询数据,同时也查询到所有符合要求得数据条数
    const getByBetweenTimesStore=async (start:Date,end:Date, current:number,pageSize:number)=>{
        allMaterialOutList.value=(await useGetByBetweenTimesAPI(start,end,current,pageSize)).data;
        allMaterialOutCounts.value=(await useGetAllCountsByBetweenTimesAPI(start,end)).data;
    }

    // 根据Id，获取一条materialOut对象
    const getOneMaterialOutById=async (id: string | RouteParamValue[])=>{
        const res=await useGetOneByIdAPI(id);
        materialOutState.value=res.data;
    }

    // 根据id，删除一条materialOut对象
    const deleteOneMaterialOutById=async (id: number | RouteParamValue[])=>{
        await useDeleteOneMaterialOutByIdAPI(id);
    }

    // 新增一条材料出库记录
    const saveOneMaterialOut=async (formData:FormData)=>{
     await useSaveOneMaterialOutAPI(formData);
    }

    // 修改一条材料出库记录
    const modifyOneMaterialOut=async (formData:FormData)=>{
        result.value=await useModifyOneMaterialOutAPI(formData);
    }



    return {
        allMaterialOutList,allMaterialOutCounts,materialOutState,
        getAllMaterialOutStore,getAllByMultifunctionNameStore,
        getByBetweenTimesStore,getOneMaterialOutById,deleteOneMaterialOutById,
        saveOneMaterialOut,modifyOneMaterialOut,result
    }


})